'''
-*-  coding:  utf-8  -*-
Time    : 20:14
Auther  :罗老师
File    :log_utils_2.py
'''
import logging.config
import os
import time


# 定义多种日志格式
class logtuils:
    standard_format = "[%(asctime)s]::[%(module)s]:[%(lineno)s]:[%(name)s]:[%(levelname)s]:[%(message)s]"
    simple_format = "%(asctime)s::%(levelname)s: %(message)s"
    id_simplie_format = "%(asctime)s:%(message)s"

    # 定义日志输出位置和日志文件名
    log_path = os.path.abspath(__file__).split("common")[0] + "data\login"
    name = time.strftime("%Y%m%d", time.localtime())
    log_name = fr"\log_{name}.log"
    log_file = log_path + log_name

    LOGGING_DIC = {
        "version": 1,  # 版本号
        "disable_existing_loggers": False,  # 固定格式
        "formatters": {
            "standard": {
                "format": standard_format
            },
            "simple": {
                "format": simple_format
            }
        },  # 日志格式
        "filters": {},  # 过滤，可以赞数不关注
        "handlers": {
            # 打印到终端的日志
            "sh": {
                "level": "INFO",
                "class": "logging.StreamHandler",
                "formatter": "simple"
            },
            # 打印到文件的日志
            "fh": {
                "level": "INFO",
                "class": "logging.handlers.RotatingFileHandler",
                "formatter": "standard",
                "filename": log_file,
                "maxBytes": 1024 * 1024,
                "backupCount": 5,
                "encoding": "utf-8"
            }
        },  # 配置日志
        "loggers": {
            "": {
                "handlers": ["sh", "fh"],
                "level": "INFO",
                "propagate": True
            }
        }  # 产生几个logger对象
    }

    def my_log(self,  name):
        logging.config.dictConfig(self.LOGGING_DIC)
        logger = logging.getLogger(name)
        return logger
